const Response = require("../../class/Response");
const LocalEvent = require("../../class/LocalEvent");

WSS.on('close', (thisClient, code, reason)=>{
    WSS.clientsManager.broadcastLogger(`<< 注销Client: ${thisClient.name} | 类型: ${thisClient.userType}`, 'warn');
    WSS.clientsManager.broadcastText(`${utils.msgHeader(thisClient.name)} §c已断开WS连接: §b${WSS.name}`);

    // 广播事件
    WSS.clientsManager.broadcastJson(new Response()
        .setUrl(CONSTANTS.EVENT.CLIENT_CLOSE)
        .setSourceId(thisClient.id)
        .setSourceBdsUuid(thisClient.bds.uuid)
        .setSourceBdsServerName(thisClient.name)
        .setStatusCode(0)
        .setStatusMessage(``)
        .setData(thisClient.getClientInfo())
    );

    // 本地事件
    WSS.api.listener.emit(CONSTANTS.EVENT.CLIENT_CLOSE, new LocalEvent()
        .setSourceId(thisClient.id)
        .setSourceBdsUuid(thisClient.bds.uuid)
        .setSourceBdsServerName(thisClient.name)
        .setStatusCode(0)
        .setStatusMessage(``)
        .setData(thisClient.getClientInfo()).json
    );
});

